package org.association.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.association.dao.AddressDao;
import org.association.dao.UserDao;
import org.association.entity.UserEntity;
import org.association.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    private AddressDao addressDao;

    @Override
    @Transactional
    public String addUser(UserEntity userEntity) {
        String address = addressDao.allocateAddress();
        if(StringUtils.isEmpty(address)) {
            return "";
        }
        // 地址被使用
        addressDao.allocate(address);

        userEntity.setAddress(address);
        save(userEntity);
        return address;
    }
}